package zut.wework.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import zut.wework.api.WorkWXAPI;
import zut.wework.api.WxApi;
import zut.wework.entity.BzksBj;
import zut.wework.entity.BzksZy;
import zut.wework.entity.Result;
import zut.wework.entity.WxDept;
import zut.wework.init.InitializationDept;
import zut.wework.mapper.BzksBjMapper;
import zut.wework.mapper.BzksZyMapper;
import zut.wework.service.IWxDeptService;

/**
 * <p>
 * 后端部门
 * </p>
 *
 * @author 刘铭心
 * @since 2022-03-22
 */
@RestController
@RequestMapping("/wework/department")
public class WxDeptController {

    @Autowired
    InitializationDept dept;

    @Autowired
    BzksZyMapper bzksZyMapper;

    @Autowired
    BzksBjMapper bzksBjMapper;

    @Autowired
    IWxDeptService wxDeptService;

    @Autowired
    WxApi wxApi;

    /**
     * 获取部门树状结构
     * @return
     */
    @GetMapping("/tree")
    public Result getTreeSelect(){
        return Result.success("success",dept.treeSelect);
    }


    /**
     * 获取年级列表
     * @return 所有年级
     */
    @GetMapping("")
    public Result getGrade(){
        return Result.success("success",dept.allMap.keySet());
    }

    /**
     * 根据年级获取院系列表
     * @param deptId 年级部门ID
     * @return 院系列表
     */
    @GetMapping("/yx")
    public Result getDeptById(int deptId){
        return Result.success("success",dept.allMap.get(deptId));
    }

    /**
     * 根据院系ID和年级ID获取专业列表
     * @param deptId 院系ID
     * @param y 年级ID
     * @return 专业列表
     */
    @GetMapping("/zy")
    public Result getZyById(String deptId,int y){
        return Result.success("success",bzksZyMapper.selectZyById(deptId,Math.abs(y-dept.year)));
    }

    /**
     * 根据专业Id和年级ID获取班级列表
     * @param deptId 专业Id
     * @param y 年级ID
     * @return 班级列表
     */
    @GetMapping("/bj")
    public Result getBjById(String deptId,int y){
        return Result.success("success",bzksBjMapper.selectList(new QueryWrapper<BzksBj>().eq("yxdm",deptId)));
    }

    /**
     * 企业微信同步接口
     * @return success
     */
    @PostMapping("/wx")
    public Result syncWxDept(){
        return wxDeptService.syncWx();
    }

    /**
     * 更新部门
     * @param wxDept 部门
     * @return ok
     */
    @PostMapping("/updateWxDept")
    public Result updateWxDept(@RequestBody WxDept wxDept){
        return wxDeptService.updateWxDept(wxDept);
    }

    /**
     * 删除部门
     * @param deptId
     * @return
     */
    @DeleteMapping("delWxDept")
    public Result delWxDept(int deptId){
        return wxDeptService.delWxDept(deptId);
    }
    /**
     * 测试
     */
    @GetMapping("/text")
    public Result getText(){
        return Result.success("success",wxApi.getAccessToken(WorkWXAPI.ADDRESS_BOOK_SECRET));
    }
}
